﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Devices.Sensors;

namespace ThunderFish
{
    /// <summary>
    /// This class provides methods for accessing the phone accelerometer
    /// </summary>
    static class TAccelerometer
    {
        private static Accelerometer accelSensor;
        private static Vector3 accelReading;
        private static Vector3 center;
        private static DateTimeOffset lastUpdate;

        private static bool doZero;

        private static Vector3 oldReading;

        private static bool accelActive;

        private static void AccelerometerReadingChanged(object sender, SensorReadingEventArgs<AccelerometerReading> e)
        {
            oldReading = accelReading;
            accelReading.X = (float)e.SensorReading.Acceleration.X;
            accelReading.Y = (float)e.SensorReading.Acceleration.Y;
            accelReading.Z = (float)e.SensorReading.Acceleration.Z;
            lastUpdate = e.SensorReading.Timestamp;

            if (doZero)
            {
                doZero = false;
                center = accelReading;
            }
        }

        static TAccelerometer()
        {
            accelSensor = new Accelerometer();

            accelSensor.CurrentValueChanged +=
                new EventHandler<SensorReadingEventArgs<AccelerometerReading>>(AccelerometerReadingChanged);
        }

        /// <summary>
        /// Get the last reading from the accelerometer.
        /// </summary>
        public static Vector3 Reading { get { return accelReading - center; } }

        /// <summary>
        /// Get the time of the last reading from the accelerometer.
        /// </summary>
        public static DateTimeOffset LastReading { get { return lastUpdate; } }

        /// <summary>
        /// Get the amount of time since the last reading from the accelerometer.
        /// </summary>
        public static TimeSpan TimeSinceLastReading { get { return DateTimeOffset.UtcNow - lastUpdate; } }

        /// <summary>
        /// Zero the accelerometer to the current reading.
        /// </summary>
        public static void ZeroAccelerometer()
        {
            doZero = true;
        }

        /// <summary>
        /// Zero the accelerometer to the given reading.
        /// </summary>
        /// <param name="zero"></param>
        public static void ZeroAccelerometer(Vector3 zero)
        {
            center = zero;
        }

        /// <summary>
        /// Start the accelerometer sensor.
        /// </summary>
        /// <returns>True if the sensor was started successfully</returns>
        public static bool Initialize()
        {
            try
            {
                accelSensor.Start();
                accelActive = true;
            }
            catch (AccelerometerFailedException e)
            {
                accelActive = false;
            }
            return accelActive;
        }

        /// <summary>
        /// Stop the accelerometer sensor.
        /// </summary>
        /// <returns>True if the sensor was stopped successfully</returns>
        public static bool Deinitialize()
        {
            bool success = false;
            if (accelActive)
            {
                try
                {
                    accelSensor.Stop();
                    success = true;
                }
                catch (AccelerometerFailedException e)
                {
                    success = false;
                }
            }
            return success;
        }
    }
}
